﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FWMS.COMMAND
{
    class Command
    {
        private static System.Threading.Thread th;
        public static string add50(string str)
        {
            if (str.Length == 0)
            {
                str = "00000";
            }
            else if (str.Length == 1)
            {
                str = "0000" + str;
            }
            else if (str.Length == 2)
            {
                str = "000" + str;
            }
            else if (str.Length == 3)
            {
                str = "00" + str;
            }
            else if (str.Length == 4)
            {
                str = "0" + str;
            }
            return str;
        }
        public static string add80(string str)
        {
            if (str.Length == 0)
            {
                str =  "00000000" + str;
            }
            else if (str.Length == 1)
            {
                str = "0000000" + str;
            }
            else if (str.Length == 2)
            {
                str = "000000" + str;
            }
            else if (str.Length == 3)
            {
                str = "00000" + str;
            }
            else if (str.Length == 4)
            {
                str = "0000" + str;
            }
            else if (str.Length == 5)
            {
                str = "000" + str;
            }
            else if (str.Length == 6)
            {
                str = "00" + str;
            }
            else if (str.Length == 7)
            {
                str = "0" + str;
            }
            return str;
        }
        public static string add20(string str)
        {
            if (str.Length == 0)
            {
                return "00";
            }
            else if (str.Length == 1)
            {
                str = "0" + str;
            }
            return str;
        }
        //判断是否是日期格式YYYYMMDD
        public static bool IsDate(string str)
        {
            try
            {
                int.Parse(str);
            }
            catch
            {
                return false;
            }
            if (str.Length != 8) return false;
            string temp;
            temp = str.Substring(0, 4) + "-" + Convert.ToString(int.Parse(str.Substring(4, 2))) + "-" + Convert.ToString(int.Parse(str.Substring(6, 2)));
            try
            {
                System.DateTime.Parse(temp);
            }
            catch
            {
                return false;
            }
            return true;
        }
        //判断是否是数值
        public static bool IsNum(string str)
        {
            try
            {
                int.Parse(str);
                return true;
            }
            catch
            {
                return false;
            }
        }
        //判断是否是LONG
        public static bool IsFloat(string str)
        {
            try
            {
                float.Parse(str);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool isFormatByReg(string param, string regPattern)
        {
            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regPattern);
            System.Text.RegularExpressions.Match m = reg.Match(param);
            if (!m.Success)
            {
                return false;
            }
            return true;

        }

        //遍历数组，查询数组中是否有相关的字符串
        public static bool bianli(object[,] Selectresult, string str, int rows)
        {
            string strcailiaoname;
            for (int i = 0; i < rows; i++)
            {
                strcailiaoname = (string)Selectresult[i, 0];
                if (str.Trim().Equals(strcailiaoname.Trim()))
                {
                    return false;
                }
            }
            return true;
        }

        public static string nullToSpace(string param)
        {
            if (param == null)
            {
                return "";
            }
            else
            {
                return param;
            }
        }

        public static Object nullToObject(Object obj)
        {
            if (obj == null)
            {
                return new object();
            }
            return obj;
        }

        public static string toString(Object obj)
        {
            if (obj == null)
            {
                return "";
            }
            else
            {
                return obj.ToString();
            }
        }

        public static int maxvalue(int value1, int value2, int value3)
        {
            if (value1 >= value2)
            {
                if (value1 >= value3)
                {
                    return value1;
                }
                else
                {
                    return value3;
                }
            }
            else
            {
                if (value2 > value3)
                {
                    return value2;
                }
                else
                {
                    return value3;
                }
            }
        }

        public static string addDay(string param)
        {
            if (param.Length != 8)
            {
                return "";
            }
            string year = param.Substring(0, 4);
            string month = param.Substring(4, 2);
            string day = param.Substring(6, 2);
            DateTime dt = new DateTime(int.Parse(year), int.Parse(month), int.Parse(day));
            dt = dt.AddDays(1);
            return dt.ToString("yyyyMMdd");
        }
        //如果传进来的字符串是空，返回零
        public static string nulltozero(object param)
        {
            if (param == null || "".Equals(param.ToString().Trim()))
            {
                return "0";
            }
            else
            {
                return param.ToString().Trim();
            }
        }
        //获取年数据
        public static string getyear(string date)
        {
            if (date.Length != 8)
            {
                return "";
            }
            string year = date.Substring(0, 4);
            return year;
        }
        //获取月数据
        public static string getmonth(string date)
        {
            if (date.Length != 8)
            {
                return "";
            }
            string month = date.Substring(4, 2);
            return month;
        }
        //获取日数据
        public static string getday(string date)
        {
            if (date.Length != 8)
            {
                return "";
            }
            string day = date.Substring(6, 2);
            return day;
        }
        //判断是用还是LFC
        public static string getLFCorYONG(string str)
        {
            if (str.Trim().Equals("内销用"))
            {
                return "内销用";
            }
            else if (str.IndexOf("用") != -1)
            {
                return "本月生产用";
            }
            else
            {
                if (str.IndexOf("LFC") != -1)
                {
                    return str;
                }
                else
                {
                    return "";
                }
            }
        }
        //
        public static void ProgressBarStart(System.Windows.Forms.ProgressBar bar)
        {
            th = new System.Threading.Thread(delegate() { bar.Value = (bar.Value++) % 100; });
            th.Start();
        }
        //
        public static void ProgressaBarStop()
        {
            if (th == null)
            {
                return;
            }
            th.Abort();
        }
    }
}
